package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.Evento;

public class PersistenceEvento {
	public static void inserirEvento(Evento evento) {
		Connection con = Conexao.getConexao();
		PreparedStatement ps;
		try {
			String query = "INSERT INTO "
					+ "eventos (nome_evento, local_evento, ini_evento, fim_evento, data_ini_evento, data_fim_evento) "
					+ "VALUES (?,?,?,?,?,?)";
			ps = con.prepareStatement(query);
			ps.setString(1, evento.getNomeEvento());
			ps.setString(2, evento.getLocalEvento());
			ps.setString(3, evento.getIniEvento());
			ps.setString(4, evento.getFimEvento());
			ps.setString(5, evento.getDataIniEvento());
			ps.setString(6, evento.getDataFimEvento());

			int status = ps.executeUpdate();
			System.out.println(status);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	public ArrayList<Evento> getToEventos(Evento evento) {
		PreparedStatement ps;
		Connection con = Conexao.getConexao();
		ArrayList<Evento> eventos = new ArrayList<Evento>();
		try {
			String query = "SELECT * FROM eventos WHERE nome_evento LIKE ? AND local_evento LIKE ? ";
			ps = con.prepareStatement(query);
			ps.setString(1, "%" + evento.getNomeEvento() + "%");
			ps.setString(2, "%" + evento.getLocalEvento() + "%");
			System.out.println(evento.getNomeEvento()+evento.getLocalEvento()+"-->");
			ResultSet rs = ps.executeQuery();
			Evento eventoTemp;
			while (rs.next()) {
				eventoTemp = new Evento();
				eventoTemp.setNomeEvento(rs.getString(2));
				eventoTemp.setLocalEvento(rs.getString(3));
				eventoTemp.setIniEvento(rs.getString(4));
				eventoTemp.setFimEvento(rs.getString(5));
				eventoTemp.setDataIniEvento(rs.getString(6));
				eventoTemp.setDataFimEvento(rs.getString(7));
				eventos.add(eventoTemp);
				eventoTemp = null;

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return eventos;
	}

	public ArrayList<Evento> getAllEventos() {
		PreparedStatement ps;
		Connection con = Conexao.getConexao();
		ArrayList<Evento> eventos = new ArrayList<Evento>();
		try {
			String query = "SELECT * FROM eventos";
			ps = con.prepareStatement(query);
			ResultSet rs = ps.executeQuery();
			Evento eventoTemp;
			while (rs.next()) {
				eventoTemp = new Evento();
				eventoTemp.setNomeEvento(rs.getString(2));
				eventos.add(eventoTemp);
				eventoTemp = null;

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return eventos;
	}
	public static void deleteEvento(Evento evento) {
		Connection con = Conexao.getConexao();
		PreparedStatement ps;
		String id_evento = null;
		try {
			String querySelect = "SELECT * FROM eventos WHERE nome_evento = ? ";
			ps = con.prepareStatement(querySelect);
			ps.setString(1, evento.getNomeEvento());
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				id_evento = rs.getString(1);
			}

			String query = "DELETE FROM eventos WHERE id_evento = " + id_evento;
			ps = con.prepareStatement(query);

			int status = ps.executeUpdate();
			System.out.println(status);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
